package com.vivo.system.service;

import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vivo.dealer.entity.ViUserDealerPrivilege;
import com.vivo.employee.entity.ViEmployee;
import com.vivo.fx.dao.SysUserdealerprivilegeDao;
import com.vivo.fx.employee.dao.FxEmployeeinfoDao;
import com.vivo.fx.employee.entity.FxEmployeeinfo;
import com.vivo.fx.employee.service.FxEmployeeinfoService;
import com.vivo.fx.entity.SysUserdealerprivilege;
import com.vivo.fx.sys.dao.FxUserinfoDao;
import com.vivo.fx.sys.entity.FxUserinfo;
import com.vivo.fx.sys.service.FxUserinfoService;
import com.vivo.system.dao.SysUserroleDao;
import com.vivo.system.entity.SysUserrole;
import com.vivo.system.entity.ViSysRoleMap;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优信通系统管理
 */
@Service
@Transactional(readOnly = true)
public class FxSysService extends BaseService implements InitializingBean {

    //    @Autowired
//    private FxUserinfoDao fxUserinfoDao;
//
//    @Autowired
//    private FxEmployeeinfoDao fxEmployeeinfoDao;
//
    @Autowired
    private FxEmployeeinfoService fxEmployeeinfoService;

    @Autowired
    private FxUserinfoService fxUserinfoService;

    @Autowired
    private SysUserroleDao sysUserroleDao;

    @Autowired
    private SysUserdealerprivilegeDao sysUserdealerprivilegeDao;


    /**
     * 同步到优信通数据
     */
    @Transactional(readOnly = false)
    public void syncUserToSys(FxEmployeeinfo fxEmp, FxUserinfo fxUser, List<SysUserrole> sysUserroleList,
                              List<SysUserdealerprivilege> privilegeList, boolean hasRoleMap, String postName) {
        if (hasRoleMap) {
            fxEmployeeinfoService.save(fxEmp);
        }

        // 只有创建新用户，才需要添加用户和添加角色
        if (fxUser.getIsNewRecord() && sysUserroleList.size() > 0) {
            fxUserinfoService.save(fxUser);
            for (SysUserrole sysUserrole : sysUserroleList) {
                sysUserroleDao.insertNew(sysUserrole);
            }
        } else if (sysUserroleList.size() > 0 && !fxUser.getUsername().contains("管理员") && !postName.equals("销售支持")) {
            // 当用户是管理员时，用户时销售支持时，不更新用权限
            // 修改用户角色
            sysUserroleDao.deleteByUserid(fxUser.getUserid());
            for (SysUserrole sysUserrole : sysUserroleList) {
                sysUserroleDao.insertNew(sysUserrole);
            }
        }

        // 清空用户经销商
        sysUserdealerprivilegeDao.deleteByUserid(fxUser.getUserid());
        saveUserPriv(privilegeList);
    }
    /**
     * 同步到优信通数据
     */
    @Transactional(readOnly = false)
    public void onlyCreateUser(FxUserinfo fxUser, List<SysUserrole> sysUserroleList) {

        // 只有创建新用户，才需要添加用户和添加角色
        if (fxUser.getIsNewRecord() && sysUserroleList.size() > 0) {
            fxUserinfoService.save(fxUser);

            // 删除可能的用户角色
            sysUserroleDao.deleteByUserid(fxUser.getUserid());

            for (SysUserrole sysUserrole : sysUserroleList) {
                sysUserroleDao.insertNew(sysUserrole);
            }
        }
        // 清空用户经销商
        sysUserdealerprivilegeDao.deleteByUserid(fxUser.getUserid());
    }

    private void saveUserPriv(List<SysUserdealerprivilege> privilegeList) {
        boolean hasPrive = true;
        int pageSize = 400;
        if (privilegeList.size() == 0) {
            return;
        }

        do {
            List<SysUserdealerprivilege> tempList = null;
            if (privilegeList.size() > pageSize) {
                tempList = privilegeList.subList(0, pageSize);
                try {
                    sysUserdealerprivilegeDao.insertByBatch(tempList);
                } catch (Exception e) {
                    e.printStackTrace();
                    for (SysUserdealerprivilege sdp : tempList) {
                        try {
                            sysUserdealerprivilegeDao.insertNew(sdp);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }
                    }
                }

                privilegeList.subList(0, pageSize).clear();
            } else {
                try {
                    sysUserdealerprivilegeDao.insertByBatch(privilegeList);
                } catch (Exception ex) {
                    ex.printStackTrace();

                    for (SysUserdealerprivilege sdp : privilegeList) {
                        try {
                            sysUserdealerprivilegeDao.insertNew(sdp);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }
                    }
                }
                privilegeList.clear();
            }

            hasPrive = privilegeList.size() > 0;
        } while (hasPrive);
    }

    /**
     * 同步到优信通数据
     */
    @Transactional(readOnly = false)
    public void syncVeToSys(FxEmployeeinfo fxEmp, boolean hasRoleMap) {
        if (hasRoleMap) {
            fxEmployeeinfoService.save(fxEmp);
        }
    }


    /**
     * 仅同步用户权限到优信通
     */
    @Transactional(readOnly = false)
    public void syncPrivToSys(FxUserinfo fxUserinfo, List<SysUserdealerprivilege> privilegeList) {
        // 清空用户经销商
        sysUserdealerprivilegeDao.deleteByUserid(fxUserinfo.getUserid());
        saveUserPriv(privilegeList);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
